<template>
  <div class="body">
    <div class="login">
      <img src="../../assets/wl/imgs/1.jpg" alt class="img" />
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from "@/api/wl";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      console.log(value);
      // let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
      // let res = reg.test(value);
      if (value) {
        callback(); //传参代表报错，不传参代码成功
      } else {
        callback(new Error("密码不能为空！"));
      }
    };
    var validateUsername = (rule, value, callback) => {
      var reg = /^[a-zA-Z0-9_-]{4,16}$/;
      let res = reg.test(value);
      if (res) {
        callback();
      } else {
        callback(new Error("4到16位（字母，数字，下划线，减号）"));
      }
    };
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 发动登陆请求
          alert("submit!"); //
          login(this.ruleForm)
            .then(res => {
              console.log(res); //查看后端向前端返回的信息

              if (res.data.state) {
                // let token=res.data.token
                let { token, userInfo } = res.data; //直接结构出来
                //1.将token存储在localstorage中
                localStorage.setItem("qftoken", token); //一开始写键值对，token名，还有Token
                // 2.登陆成功后跳转到home页
                this.$router.push("/");
              } else {
                // 密码错误
                alert("账号密码错误"); //这一步是由于是在后端注册这个账号之后，后端会存储这个用户信息，当新用户登陆时会等不上，必须走注册接口，但这个接口在老师的库中，没有
              }
            })
            .catch(err => {
              // 网络超时或者请求错误
              alert("网络超时或者请求错误"); //这一步由于可能走不到.then所以，此时得做判断
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style scoped>
.body {
  height: 100%;
  width: 100%;
  position: relative;
  background-image: url(../../assets/wl/imgs/1.jpg);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-attachment: fixed;
}
.login {
  width: 400px;
  height: 400px;
  position: absolute;
  left: 500px;
  top: 200px;
}
.login .img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: block;
  margin-left: 200px;
}
</style>


